package practice1_100;

public class Practice2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int count = 0;
        int value=0;
        ListNode backList = new ListNode();
        ListNode pointList = backList;
        while (l1 != null && l2 != null) {
             value = l1.val + l2.val + count;
            if (value >= 10) {
                value = value - 10;
                count = 1;
                pointList.val = value;
            } else {
                count = 0;
                pointList.val = value;
            }
            if(l1.next!=null||l2.next!=null){
                pointList.next = new ListNode();
                pointList = pointList.next;
            }
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null) {
             value = l1.val + count;
            if (value >= 10) {
                value = value - 10;
                count = 1;
                pointList.val = value;
            } else {
                count = 0;
                pointList.val = value;
            }
            if(l1.next!=null){
                pointList.next = new ListNode();
                pointList = pointList.next;
            }
        }
        while (l2 != null) {
             value = l1.val + count;
            if (value >= 10) {
                value = value - 10;
                count = 1;
                pointList.val = value;
            } else {
                count = 0;
                pointList.val = value;
            }
            if(l2.next!=null){
                pointList.next = new ListNode();
                pointList = pointList.next;
            }
        }
        return backList;

    }

    //自己写的算法太烂了，看看别人咋做的
    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        int sum=0;
        int count=0;
        ListNode head =new ListNode();
        ListNode current=head;
        int value1=0;
        int value2=0;
        while (l1!=null||l2!=null){
            //先两数之和取尾
             value1= l1==null?0:l1.val;
             value2= l2==null?0:l2.val;

             //先取两个数和进一位的数相加
            sum=value1+value2+count;
            // 取余后赋值新节点存储
            current.val=sum%10;
            //取模算出进一位的数大小；
            count=sum/10;

            if(l1!=null)
                l1=l1.next;
            if(l2!=null)
                l2=l2.next;
//            if(l1!=null||l2!=null) {
            current.next = new ListNode();
            current=current.next;
//            }
        }
        if(count==1)
            current.next=new ListNode(count);
        return head;
    }
    public static void main(String[] args) {
        //[2,4,3]
        //[5,6,4]
        ListNode listNode=new ListNode();
        listNode.val=9;
        listNode.next=new ListNode();
        listNode.next.val=9;
        listNode.next.next=new ListNode();
        listNode.next.next.val=9;

        ListNode listNode1=new ListNode();
        listNode1.val=9;
        listNode1.next=new ListNode();
        listNode1.next.val=9;
//        listNode1.next.next=new ListNode();
//        listNode1.next.next.val=9;

        Practice2 practice2=new Practice2();
        ListNode listNode2= practice2.addTwoNumbers2(listNode,listNode1);
        System.out.println(listNode2);

    }

}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
